var TSUI;
(function (TSUI) {
    (function (Animation) {
        var AppWindowAnimator = (function () {
            function AppWindowAnimator() {
                this._CanvasElem = null;
            }
            AppWindowAnimator.prototype.Show = function (control, callback) {
                if (typeof callback === "undefined") { callback = null; }
                var useCanvasRender = this._EvaluateUseCanvasRender(control);
                var OK = false;
                if (useCanvasRender) {
                    OK = true;
                    try  {
                        var element = control.AnimationElement();
                        if (this._CanvasElem === null) {
                            var _this = this;
                            Animation.AnimationHelper.PrepareHTML2CanvasForAnimation(element, function (canvas) {
                                _this.DoHTML2CanvasShow(element, canvas, callback);
                            });
                        } else {
                            this.DoHTML2CanvasShow(element, this._CanvasElem, callback);
                        }
                    } catch (ex) {
                        OK = false;
                        AppWindowAnimator.UseCanvasRenderIfSensible = false;
                    }
                }
                if (!OK) {
                    var element = control.AnimationElement();
                    var startSize = TSUI.GetSize(control);
                    var startWidth = startSize.width;
                    var startHeight = startSize.height;
                    var startPos = TSUI.GetPosition(control);
                    var origCSSTop = ((startPos.top / element.parent().height()) * 100).toString() + "%";
                    var origCSSLeft = ((startPos.left / element.parent().width()) * 100).toString() + "%";

                    element.css({
                        width: startWidth - (startWidth * AppWindowAnimator.ShrinkAmount * 2),
                        height: startHeight - (startHeight * AppWindowAnimator.ShrinkAmount * 2),
                        left: element.parent().width() + 50,
                        visibility: "",
                        display: ""
                    });

                    element.css({
                        top: startPos.top + (startHeight * AppWindowAnimator.ShrinkAmount)
                    }).animate({
                        left: startPos.left + (startWidth * AppWindowAnimator.ShrinkAmount),
                        opacity: 1
                    }, AppWindowAnimator.FlyTime, AppWindowAnimator.AnimationEasing).delay(AppWindowAnimator.DelayTime).animate({
                        width: startWidth,
                        height: startHeight,
                        top: origCSSTop,
                        left: origCSSLeft,
                        visibility: "",
                        display: ""
                    }, AppWindowAnimator.ResizeTime, callback);
                }
            };

            AppWindowAnimator.prototype.Hide = function (control, callback) {
                if (typeof callback === "undefined") { callback = null; }
                var useCanvasRender = this._EvaluateUseCanvasRender(control);
                var OK = false;
                if (useCanvasRender) {
                    OK = true;
                    try  {
                        var element = control.AnimationElement();
                        if (this._CanvasElem === null) {
                            var _this = this;
                            Animation.AnimationHelper.PrepareHTML2CanvasForAnimation(element, function (canvas) {
                                _this.DoHTML2CanvasHide(element, canvas, callback);
                            });
                        } else {
                            this.DoHTML2CanvasHide(element, this._CanvasElem, callback);
                        }
                    } catch (ex) {
                        OK = false;
                        AppWindowAnimator.UseCanvasRenderIfSensible = false;
                    }
                }
                if (!OK) {
                    var element = control.AnimationElement();
                    var startSize = TSUI.GetSize(control);
                    var startWidth = startSize.width;
                    var startHeight = startSize.height;
                    var startPos = TSUI.GetPosition(control);

                    element.animate({
                        width: startWidth - (startWidth * AppWindowAnimator.ShrinkAmount * 2),
                        height: startHeight - (startHeight * AppWindowAnimator.ShrinkAmount * 2),
                        top: startPos.top + (startHeight * AppWindowAnimator.ShrinkAmount),
                        left: startPos.left + (startWidth * AppWindowAnimator.ShrinkAmount)
                    }, AppWindowAnimator.ResizeTime).delay(AppWindowAnimator.DelayTime).animate({
                        left: element.parent().width() + 50,
                        opacity: 0
                    }, AppWindowAnimator.FlyTime, AppWindowAnimator.AnimationEasing, function () {
                        element.css({
                            width: startWidth,
                            height: startHeight,
                            top: startPos.top,
                            left: startPos.left,
                            display: "",
                            visibility: "hidden"
                        });

                        if (callback) {
                            callback();
                        }
                    });
                }
            };

            AppWindowAnimator.prototype.DoHTML2CanvasShow = function (element, canvas, callback) {
                var startWidth = parseInt(canvas.css("width"), 10);
                var startHeight = parseInt(canvas.css("height"), 10);
                var startPos = {
                    top: parseInt(canvas.css("top"), 10),
                    left: parseInt(canvas.css("left"), 10)
                };
                var _this = this;

                canvas.css({
                    width: startWidth - (startWidth * AppWindowAnimator.ShrinkAmount * 2),
                    height: startHeight - (startHeight * AppWindowAnimator.ShrinkAmount * 2),
                    left: element.parent().width() + 50,
                    top: startPos.top + (startHeight * AppWindowAnimator.ShrinkAmount),
                    opacity: 0,
                    visibility: "",
                    display: ""
                }).animate({
                    left: startPos.left + (startWidth * AppWindowAnimator.ShrinkAmount),
                    opacity: 1
                }, AppWindowAnimator.FlyTime, AppWindowAnimator.AnimationEasing).delay(AppWindowAnimator.DelayTime).animate({
                    width: startWidth,
                    height: startHeight,
                    top: startPos.top,
                    left: startPos.left,
                    visibility: "",
                    display: ""
                }, AppWindowAnimator.ResizeTime, function () {
                    Animation.AnimationHelper.RestoreAfterAnimationHTML2Canvas(element, canvas);

                    _this._CanvasElem = null;

                    if (callback !== null) {
                        callback();
                    }
                });
            };

            AppWindowAnimator.prototype.DoHTML2CanvasHide = function (element, canvas, callback) {
                var startWidth = parseInt(canvas.css("width"), 10);
                var startHeight = parseInt(canvas.css("height"), 10);
                var startPos = {
                    top: parseInt(canvas.css("top"), 10),
                    left: parseInt(canvas.css("left"), 10)
                };
                canvas.css("display", "block");
                element.css("display", "none");

                canvas.animate({
                    width: startWidth - (startWidth * AppWindowAnimator.ShrinkAmount * 2),
                    height: startHeight - (startHeight * AppWindowAnimator.ShrinkAmount * 2),
                    top: startPos.top + (startHeight * AppWindowAnimator.ShrinkAmount),
                    left: startPos.left + (startWidth * AppWindowAnimator.ShrinkAmount)
                }, AppWindowAnimator.ResizeTime).delay(AppWindowAnimator.DelayTime).animate({
                    left: element.parent().width() + 50,
                    opacity: 0
                }, AppWindowAnimator.FlyTime, AppWindowAnimator.AnimationEasing, function () {
                    canvas.remove();
                    element.css({
                        width: startWidth,
                        height: startHeight,
                        top: startPos.top,
                        left: startPos.left,
                        display: "",
                        visibility: "hidden"
                    });

                    if (callback) {
                        callback();
                    }
                });
            };

            AppWindowAnimator.prototype._EvaluateUseCanvasRender = function (control) {
                var use = AppWindowAnimator.UseCanvasRenderIfSensible;

                if (use) {
                    var elem = control.AnimationElement();
                    use = elem.width() * elem.height() > 240000;
                    var children = elem.find("*");
                    if (use) {
                        use = children.length > 15;
                    } else {
                        use = children.length > 50;
                    }
                }

                return use;
            };

            AppWindowAnimator.prototype.PrepareHTML2CanvasForAnimation = function (control, callback) {
                if (typeof callback === "undefined") { callback = null; }
                if (this._EvaluateUseCanvasRender(control)) {
                    try  {
                        var _this = this;
                        _this._CanvasElem = null;
                        var elem = control.AnimationElement();
                        Animation.AnimationHelper.PrepareHTML2CanvasForAnimation(elem, function (canvas) {
                            _this._CanvasElem = canvas;

                            if (callback) {
                                callback();
                            }
                        });
                    } catch (ex) {
                        AppWindowAnimator.UseCanvasRenderIfSensible = false;
                        _this._CanvasElem = null;

                        if (callback) {
                            callback();
                        }
                    }
                } else {
                    if (callback) {
                        callback();
                    }
                }
            };
            AppWindowAnimator.FlyTime = 500;

            AppWindowAnimator.ResizeTime = 100;

            AppWindowAnimator.DelayTime = 50;

            AppWindowAnimator.ShrinkAmount = 0.01;

            AppWindowAnimator.AnimationEasing = "easeOutCubic";

            AppWindowAnimator.UseCanvasRenderIfSensible = false;
            return AppWindowAnimator;
        })();
        Animation.AppWindowAnimator = AppWindowAnimator;
    })(TSUI.Animation || (TSUI.Animation = {}));
    var Animation = TSUI.Animation;
})(TSUI || (TSUI = {}));
